﻿using FeedbackNetwork.network;
using System;
using System.Collections;

namespace FeedbackNetwork.dataset
{
    class MnistDataset:Dataset
    {
        public MnistDataset()
        {
           
        }

        public void ReadDataFromFile(string image_file, string label_file)
        {
            ArrayList idx = MnistFileParser.ParseLabelFile(label_file);
            ArrayList image = MnistFileParser.ParseImageFile(image_file);

            if (idx.Count != image.Count)
            {
                Console.WriteLine("the number of labels and the number of images are not match!");
                return;
            }

            for (int i = 0; i < idx.Count; i++)
            {
                MnistData temp = new MnistData((uint)idx[i], (float[])image[i]);
                this.AddData(temp);
            }
        }

        public static FloatTensor GetImageTensor(ArrayList data)
        {
            int[] shape = new int[] { data.Count, 28 * 28 };
            FloatTensor result = new FloatTensor(shape, 0f);

            for(int i = 0; i < data.Count; i++)
            {
                MnistData temp = (MnistData)data[i];
                result.SetDimensionData(0, i, temp.GetImageTensor());
            }
            if (float.IsNaN(result.Sum()))
                Console.WriteLine("NAN出现了");
            return result;
        }

        public static FloatTensor GetLabelTensor(ArrayList data)
        {
            int[] shape = new int[] { data.Count, 10 };
            FloatTensor result = new FloatTensor(shape, 0f);

            for (int i = 0; i < data.Count; i++)
            {
                MnistData temp = (MnistData)data[i];
                result.SetDimensionData(0, i, temp.GetLabelTensor());
            }
            return result;
        }

    }
}
